package org.mch.func.db.conn;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import org.mch.biz.ui.FuncEvent;
import org.mch.biz.ui.FuncPanel;
import org.mch.func.cons.ConsDB;
import org.mch.swingm.core.MComboBox;

public class ConnDBPanel extends FuncPanel {
	private static final long serialVersionUID = 1L;

	private JPanel panel_conn;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	private JPasswordField passwordField;
	private MComboBox<String> comboBox_username;
	private MComboBox<String> comboBox_dbport;
	private MComboBox<String> comboBox_dbaddress;
	private MComboBox<String> comboBox_dbtype;
	private JButton button_conn;
	private JLabel label_7;
	private JLabel label_9;
	private MComboBox<String> comboBox_dbname;

	public ConnDBPanel() {

		setBounds(100, 100, 641, 476);
		this.setLayout(new BorderLayout());
		this.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.setLayout(new BorderLayout(0, 0));
		this.add(getPanel_conn(), BorderLayout.CENTER);
		JPanel buttonPane = new JPanel();
		this.add(buttonPane, BorderLayout.SOUTH);
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
	}

	public JPanel getPanel_conn() {
		if (this.panel_conn == null) {
			this.panel_conn = new JPanel();
			this.panel_conn.setLayout(null);
			this.panel_conn.add(getLabel_9());
			this.panel_conn.add(getLabel_1_1());
			this.panel_conn.add(getLabel_2_1());
			this.panel_conn.add(getLabel_3_1());
			this.panel_conn.add(getLabel_4_1());
			this.panel_conn.add(getPasswordField());
			this.panel_conn.add(getComboBox_username());
			this.panel_conn.add(getComboBox_dbport());
			this.panel_conn.add(getComboBox_dbaddress());
			this.panel_conn.add(getComboBox_dbtype());
			this.panel_conn.add(getButton_conn());
			this.panel_conn.add(getLabel_7_1());
			this.panel_conn.add(getLabel_9_1());
			this.panel_conn.add(getComboBox_dbname());
		}

		return this.panel_conn;
	}

	public JLabel getLabel_9() {
		if (this.label == null) {
			this.label = new JLabel("数据库类型：");
			this.label.setBounds(11, 12, 91, 15);
		}
		return this.label;
	}

	public JLabel getLabel_1_1() {
		if (this.label_1 == null) {
			this.label_1 = new JLabel("地址：");
			this.label_1.setBounds(11, 49, 91, 15);
		}
		return this.label_1;
	}

	public JLabel getLabel_2_1() {
		if (this.label_2 == null) {
			this.label_2 = new JLabel("端口：");
			this.label_2.setBounds(11, 83, 91, 19);
		}
		return this.label_2;
	}

	public JLabel getLabel_3_1() {
		if (this.label_3 == null) {
			this.label_3 = new JLabel("用户名：");
			this.label_3.setBounds(11, 152, 91, 15);
		}
		return this.label_3;
	}

	public JLabel getLabel_4_1() {
		if (this.label_4 == null) {
			this.label_4 = new JLabel("密码：");
			this.label_4.setBounds(11, 187, 91, 15);
		}
		return this.label_4;
	}

	public JPasswordField getPasswordField() {
		if (this.passwordField == null) {
			this.passwordField = new JPasswordField();
			this.passwordField.setBounds(112, 184, 177, 21);
			this.passwordField.setText("a");
		}
		return this.passwordField;
	}

	public MComboBox<String> getComboBox_username() {
		if (this.comboBox_username == null) {
			this.comboBox_username = new MComboBox<String>();
			comboBox_username.setModel(new DefaultComboBoxModel<String>(new String[] { "root", "mch" }));
			this.comboBox_username.setBounds(112, 148, 177, 23);
			this.comboBox_username.setEditable(true);
		}
		return this.comboBox_username;
	}

	public MComboBox<String> getComboBox_dbport() {
		if (this.comboBox_dbport == null) {
			this.comboBox_dbport = new MComboBox<String>();
			this.comboBox_dbport.setBounds(112, 81, 177, 23);
			this.comboBox_dbport.setEditable(true);
			for (int i = 0; i < ConsDB.dbport.length; i++) {
				this.comboBox_dbport.addItem(ConsDB.dbport[i]);
			}
		}
		return this.comboBox_dbport;
	}

	public MComboBox<String> getComboBox_dbaddress() {
		if (this.comboBox_dbaddress == null) {
			this.comboBox_dbaddress = new MComboBox<String>();
			this.comboBox_dbaddress.setModel(new DefaultComboBoxModel<String>(new String[] { "localhost" }));
			this.comboBox_dbaddress.setBounds(112, 45, 177, 23);
			this.comboBox_dbaddress.setEditable(true);
		}
		return this.comboBox_dbaddress;
	}

	public MComboBox<String> getComboBox_dbtype() {
		if (this.comboBox_dbtype == null) {
			this.comboBox_dbtype = new MComboBox<String>();
			comboBox_dbtype.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					if (e.getStateChange() == ItemEvent.SELECTED) {
						Object obj = e.getItem();
						getComboBox_dbport().setSelectedItem(ConsDB.portMap.get(obj.toString()));
					}
				}
			});
			this.comboBox_dbtype.setBounds(112, 8, 177, 23);
			for (int i = 0; i < ConsDB.dbtype.length; i++) {
				this.comboBox_dbtype.addItem(ConsDB.dbtype[i]);
			}
			this.comboBox_dbtype.setSelectedItem(ConsDB.dbtype[4]);
		}
		return this.comboBox_dbtype;
	}

	public JButton getButton_conn() {
		if (this.button_conn == null) {
			this.button_conn = new JButton("连接");
			this.button_conn.setBounds(213, 215, 74, 23);
			this.button_conn.addActionListener(getEvent());
		}
		return this.button_conn;
	}

	public JLabel getLabel_7_1() {
		if (this.label_7 == null) {
			this.label_7 = new JLabel("--------------------------------------------");
			this.label_7.setBounds(11, 238, 278, 15);
		}
		return this.label_7;
	}

	public JLabel getLabel_9_1() {
		if (this.label_9 == null) {
			this.label_9 = new JLabel("数据库名称：");
			this.label_9.setBounds(10, 112, 84, 30);
		}
		return this.label_9;
	}

	public MComboBox<String> getComboBox_dbname() {
		if (this.comboBox_dbname == null) {
			this.comboBox_dbname = new MComboBox<String>();
			comboBox_dbname.setModel(new DefaultComboBoxModel<String>(new String[] { "temp" }));
			this.comboBox_dbname.setBounds(112, 114, 177, 23);
			this.comboBox_dbname.setEditable(true);
		}
		return this.comboBox_dbname;
	}

	@Override
	protected FuncEvent createEvent() {
		return new ConnDBEvent(this);
	}
}
